<?php
/**
 * 《生产信息》实现类
 *
 * @author 孙晓晔
 * @version $Id: Equipment.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/CarUtil.class.php');

class Produce extends CarUtil {
	
	public $tbl = Constant::tbl_cnc_produce;
	
	public $shift = array(
		'-' => '请选择',

		'1' => '白班',
		'2' => '夜班',

		'9' => '其它',
	);
	
	public $day = array(
		'-' => '请选择',

		'1' => '昨天',
		'2' => '今天',
		'3' => '明天',
		'4' => '上周',
		'5' => '本周',

		// '9' => '其它',
	);

	public $status = array(
		'-' => '请选择',
		
		'1' => '待料生产',
		'2' => '正在生产',
			
		'3' => '等待调机',
		'4' => '正在调机',
			
		'5' => '正在维修',
		'6' => '停机',
	
		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'emp' => '负责人',
		'date' => '日期',
	
		'machine' => '机台',
		
		'customer' => '客户',
		'project' => '项目',
		'mold' => '产品',
			
		'status' => '状态',
	
		'1' => '创建时间',
		'2' => '更新时间',
	);
	
	public $groupby = array(
		'-' => '请选择',
		'emp' => '负责人',
	
		'machine' => '机台',
		'customer' => '客户',
		'project' => '项目',
		'mold' => '产品',
			
		'status' => '状态',
		'2' => '日期',
		'3' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
		
		$controller->assign_by_ref('shift_option', $this->shift);
		$controller->assign_by_ref('day_option', $this->day);
	}
	
	function tab($controller, $request) {
		$data = array(
			'生产信息' => array('cc'=>'/b/admin/car.php?do=Produce.find', 'js'=>''),
			'机台信息' => array('cc'=>'/b/admin/car.php?do=Cnc.find', 'js'=>''),

			'统计分析' => array('cc'=>'/b/admin/car.php?do=Produce.tj_tab', 'js'=>''),
		);
	
		$ret = $this->_tab($data);
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'tab');
		return true;
	}
	
	function tj_tab($controller, $request) {
		$data = array(
			'生产信息' => array('cc'=>'/b/admin/car.php?do=Produce.group', 'js'=>''),
			'机台信息' => array('cc'=>'/b/admin/car.php?do=Cnc.group', 'js'=>''),
		);
	
		$ret = $this->_tab($data);
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'tj_tab');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);
		
		$controller->assign('orderby_s', 'machine');
		$controller->assign('desc_s', '1');
		
		$controller->assign('pagesize_s', '50');

		$controller->display($request, 'car/produce/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$machine_s = $this->_input($request['machine_s']);
		$name_s = $request['name_s'];

		$date1_s = $request['date1_s'];
		$date2_s = $request['date2_s'];
		
		$where = " WHERE 1=1";

		if (!empty($machine_s)) {
			$where .= " AND machine = '" . $machine_s . "'";
		}
		
		if (!empty($date1_s)) {
			$where .= " AND date >= '" . $date1_s . "'";
		}

		if (!empty($date2_s)) {
			$where .= " AND date < '" . $date2_s . "'";
		}

		if (!empty($name_s)) {
			$where .= " AND name LIKE '%" . $name_s . "%'";
		}
		
		$where .= $this->_where_x("shift", $request['shift_s']);

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'car/produce/list');
		return true;
	}

	function xls($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);
		
		$request['tpl'] = "xls";

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);
		
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);

			$row = $this->_orderby_x($controller, $row, $this->orderby_s);

			$data[] = $row;
		}

		$controller->assign_by_ref('result', $data);
		
		$filename = sprintf("%s.xls", date("YmdHis"));
		header('Content-Disposition:application/vnd.ms-excel; filename=' . $filename);

		$controller->display($request, 'car/produce/list');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function my($controller, $request) {

		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'car/produce/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$date = $request['date'];
		
		$machine = $this->_input($request['machine']);
		$shift = $request['shift'];
		
		$mold = $this->_input($request['mold']);
		$mold_t = Name::mold($mold);
		$customer = $this->_customer($mold_t);
		$project = $this->_project($mold_t);
		
		$output = $request['output'];
		$quantity = $request['quantity'];
		
		$name = addslashes($request['name']);
		$content = addslashes($request['content']);
		
		$status = $request['status'];

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, dept, emp, date, machine, shift, customer, project, mold, output, quantity, name, content, oper, status, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $date, $machine, $shift, $customer, $project, $mold, $output, $quantity, $name, $content, $_SESSION['id'], $status);
		debug($sql);
		$db->query($sql);

		$id = $db->insert_id();

		$request['id_s'] = $id;
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'car/produce/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$date = $request['date'];
		
		$machine = $this->_input($request['machine']);
		$shift = $request['shift'];
		
		$mold = $this->_input($request['mold']);
		$mold_t = Name::mold($mold);
		$customer = $this->_customer($mold_t);
		$project = $this->_project($mold_t);
		
		$output = $request['output'];
		$quantity = $request['quantity'];
		
		$name = addslashes($request['name']);
		$content = addslashes($request['content']);

		$status = $request['status'];
		
		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET office = '%s', dept = '%s', emp = '%s', date = '%s', machine = '%s', shift = '%s', customer = '%s', project = '%s', mold = '%s'";
		$fmt .= ", output = '%s', quantity = '%s', name = '%s', content = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $date, $machine, $shift, $customer, $project, $mold, $output, $quantity, $name, $content, $status, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'car/produce/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['shift_t'] = $this->shift[$row['shift']];
		$row['week'] = $this->week($row['date']);
		
		$row['machine_t'] = Name::machine($row['machine']);
		
		$row = $this->_view_t($row);

		return $row;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function copy($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'car/produce/copy');
		return true;
	}
	
	function batch($controller, $request) {
		$this->pretreat($controller);
		
		$row = json_encode($this->_cnc());
		
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'car/produce/batch');
		return true;
	}
	
	function batchPost($controller, $request) {
		$this->pretreat($controller);
		
		$shift = $request['shift'];
		$date = $request['date'];
		
		$data = json_decode($request['item'], true);
				
		$db = newdb($this->dsn_w);		
		$fmt = "INSERT INTO %s(office, dept, emp, date, machine, shift, customer, project, mold, name, output, content, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		while (list($k, $v) = each($data)) {
			
			if (empty($v['emp']) || empty($v['cnc'])) {
				continue;
			}
			
			$machine = $this->_input($v['cnc']);
			
			$emp = $this->_input($v['emp']);
			$office = $this->_office($emp);
			$dept = $this->_emp_dept($emp);
			
			$mold = $this->_input($v['mold_']);
			$mold_t = Name::mold($mold);
			$customer = $this->_customer($mold_t);
			$project = $this->_project($mold_t);
			
			$name = addslashes($v['name']);
			$content = addslashes($v['content']);
			
			$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $date, $machine, $shift, $customer, $project, $mold, $name, $v['output'], $content, $_SESSION['id']);
			debug($sql);
			$db->query($sql);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function update($controller, $request) {
		$this->pretreat($controller);
		
		$insql = "in ('000000000";
		while (list($k, $v) = each($request)) {
			if (substr($k, 0, 2) == "c_") {
				$insql .= "', '" . $v;
			}
		}
		$insql .= "')";
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE id %s ORDER BY machine";
		$sql = sprintf($fmt, $this->tbl, $insql);
		debug($sql);
		
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);
			
			$t = array();
			$t['id'] = $row['id'];
			
			$t['shift'] = $row['shift'];
			$t['date'] = $row['date'];
			$t['status'] = $row['status'];
			
			
			$t['emp'] = sprintf("%s:%s", $row['emp'], $row['emp_t']);
			$t['cnc'] = sprintf("%s:%s", $row['machine'], $row['machine_t']);
			$t['mold_'] = sprintf("%s:%s", $row['mold'], $row['mold_t']);
			
			$t['output'] = $row['output'];
			
			$t['name'] = $row['name'];
			$t['content'] = $row['content'];
			
			$data[] = $t;
				
			// $data[] = $row;
		}

		$controller->assign_by_ref('result', $data[0]);
		
		$controller->assign_by_ref('data', json_encode($data));
		
		$controller->display($request, 'car/produce/update');
		return true;
	}
	
	function updatePost($controller, $request) {
		$this->pretreat($controller);
		
		debug(print_r($request, true));
		
		$shift = $request['shift'];
		$date = $request['date'];
		$status = $request['status'];
		
		$content = addslashes($request['content']);
		
		$data = json_decode($request['item'], true);
		
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET office = '%s', dept = '%s', emp = '%s', date = '%s', machine = '%s', shift = '%s', customer = '%s', project = '%s', mold = '%s'";
		$fmt .= ", output = '%s', name = '%s', content = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		
		while (list($k, $v) = each($data)) {
			
			if (empty($v['emp']) || empty($v['cnc'])) {
				continue;
			}
			
			$machine = $this->_input($v['cnc']);
			
			$emp = $this->_input($v['emp']);
			$office = $this->_office($emp);
			$dept = $this->_emp_dept($emp);
			
			$mold = $this->_input($v['mold_']);
			$mold_t = Name::mold($mold);
			$customer = $this->_customer($mold_t);
			$project = $this->_project($mold_t);
			
			$name = addslashes($v['name']);
			$content = addslashes($v['content']);
			
			$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $date, $machine, $shift, $customer, $project, $mold, $v['output'], $name, $content, $status, $v['id']);
			debug($sql);
			$db->query($sql);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function batch_copy($controller, $request) {
		$this->pretreat($controller);
	
		$insql = "in ('000000000";
		while (list($k, $v) = each($request)) {
			if (substr($k, 0, 2) == "c_") {
				$insql .= "', '" . $v;
			}
		}
		$insql .= "')";
	
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE id %s ORDER BY machine";
		$sql = sprintf($fmt, $this->tbl, $insql);
		debug($sql);
	
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);
				
			$t = array();
			$t['id'] = $row['id'];
				
			$t['shift'] = $row['shift'];
			$t['date'] = $row['date'];
			$t['status'] = $row['status'];
				
				
			$t['emp'] = sprintf("%s:%s", $row['emp'], $row['emp_t']);
			$t['cnc'] = sprintf("%s:%s", $row['machine'], $row['machine_t']);
			$t['mold_'] = sprintf("%s:%s", $row['mold'], $row['mold_t']);
				
			$t['output'] = $row['output'];
				
			$t['name'] = $row['name'];
			$t['content'] = $row['content'];
				
			$data[] = $t;
	
			// $data[] = $row;
		}
		
		$controller->assign_by_ref('result', $data[0]);
	
		$controller->assign_by_ref('data', json_encode($data));
	
		$controller->display($request, 'car/produce/batch_copy');
		return true;
	}
	
	function batch_view($controller, $request) {
		$this->pretreat($controller);
		
		$this->findPara($controller, $request);
	
		$insql = "in ('000000000";
		while (list($k, $v) = each($request)) {
			if (substr($k, 0, 2) == "c_") {
				$insql .= "', '" . $v;
			}
		}
		$insql .= "')";
	
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE id %s %s";
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $insql, $orderby);
		debug($sql);
	
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);
			$data[] = $row;
		}
	
		$controller->assign_by_ref('result', $data[0]);
		
		$controller->assign_by_ref('data', $data);
	
		$controller->display($request, 'car/produce/batch_view');
		return true;
	}
	
	function _cnc() {
		require_once(dirname(__FILE__) . '/Cnc.class.php');
		$aa = new Cnc();
		
		$a = $aa->_list();
	
		$data = array();
		while (list($k, $v) = each($a)) {
			$t = array();
	
			$t['id'] = $v['id'];
			$t['code'] = $v['code'];
			$t['cnc'] = sprintf("%s:%s", $v['id'], $v['code']);
			
			$data[] = $t;
		}
	
		return $data;
	}
	
	function group($controller, $request) {
		$this->pretreat_group($controller);
	
		// $controller->assign('t1', strtotime('-1 month'));
	
		$num = array(
			'1' => '按次数',
			'2' => '按产能',
		);
		$controller->assign('num_option', $num);
	
		$controller->display($request, 'car/produce/group');
		return true;
	}
	
	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'machine');
	
		$controller->assign('groupby', $this->groupby[$px]);
	
		return $this->groupby_dd($px);
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function groupPost($controller, $request) {
		$this->pretreat($controller);
			
		$this->groupPara($controller, $request);
		
		$num_s = $request['num_s'];
		if ($num_s == '1') {
			$fmt = "SELECT %s AS a, COUNT(*) AS c FROM %s a ";
		} elseif ($num_s == '2') {
			$fmt = "SELECT %s AS a, SUM(quantity) AS c FROM %s a ";
		} else {
			$fmt = "SELECT %s AS a, SUM(quantity) AS c FROM %s a ";
		}
		$sql = sprintf($fmt, $this->s, $this->tbl);
		
		$where = $this->_where($request);
		
		$request['sql'] = sprintf("%s %s GROUP BY %s ORDER BY %s %s", $sql, $where, $this->s, $this->orderby, $this->desc);
		
		$this->_group($controller, $request);
	
		// $controller->display($request, 'list');
		return true;
	}
	
	function groupby_dd($px) {
		if ($px == '2') {
			return "DATE_FORMAT(date, '%Y-%m-%d')";
		}
	
		if ($px == '3') {
			return "DATE_FORMAT(date, '%Y-%m')";
		}
	
		if ($px == '4') {
			return "DATE_FORMAT(date, '%Y')";
		}
	
		return $px;
	}
	
	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'machine') {
			$row['a'] = sprintf("%s [%s]", Name::machine($row['a']), $row['a']);
		} elseif ($px == 'type') {
			$row['a'] = $this->type[$row['a']];
		} elseif ($px == 'customer') {
			// $row['a'] = $row['a'];
			$a = 1;
		} else {
	
			$row = $this->_name_t($row, $px);
		}
	
		return $row;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function ajax_code($controller, $request) {
		
		$data = $this->_ajax_code($request);
		echo json_encode($data);
	}

	function _ajax_code($request) {
		$q = $request['q'];
		$n = $request['maxRows'];
		if (empty($n)) {
			$n = 20;
		}
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE status = '1' AND code LIKE '%s' ORDER BY id DESC";
		$sql = sprintf($fmt, $this->tbl, $q . "%");
		// debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row['emp_t'] = Name::emp($row['emp']);

			$ret[] = $row;
		}

		return $ret;
	}
	
	function ajax_b($controller, $request) {
		$id = $_COOKIE['id_'];
		$ret = array();
	
		if ($this->_ajax_x($id, 'emp')) {
			$ret['status'] = 1;
		} elseif ($this->_ajax_x($id, 'oper')) {
			$ret['status'] = 1;
		} elseif ($this->_audit('15')) {
			$ret['status'] = 1;
		} elseif ($this->_is_admin()) {
			$ret['status'] = 1;
		} else {
			$ret['status'] = 2;
		}
	
		echo json_encode($ret);
	}
	
	function z($controller, $request) {
		$this->pretreat($controller);
	
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s ";
		$sql = sprintf($fmt, $this->tbl);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$mold_t = Name::mold($row['mold']);
			
			$customer = $this->_customer($mold_t);
			$project = $this->_project($mold_t);
	
			$fmt = "UPDATE %s SET customer = '%s', project = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $customer, $project, $row['id']);
			echo $sql . "\n";
			$db->query($sql);
		}
	
		logdate($controller, $request);
		return true;
	}

	function u($controller, $request) {
		logdate($controller, $request);
		return true;
	}
}
?>